﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using System.Data;
using EnterpriseFund.Common;
using DevExpress.Web.ASPxGridView;
using DevExpress.XtraReports.UI;

namespace EnterpriseFund.Controllers
{
    public class QueryAcceptController : Controller
    {
        //
        // GET: /QueryAccept/

        public ActionResult Index()
        {
            BLL.Common CommonBLL = new BLL.Common();
            string year = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();
            string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
            string strwhere1 = "";
            if (str != "")
            {
                strwhere1 = @"CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
 NOT in
(SELECT  CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
         FROM fund.V_SB_ZSXX WHERE" + str + @") and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            else
            {
                strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            ViewBag.SKSJA = "40";
            ViewData["se"] = 40;
            string strwhere2 = " g.se>400000 ";
            ViewData["year"] = year;
            ViewBag.dyear = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            DataSet ds = VSBZSXXBLL.GetAllowanceWaitListWithAllwance(strwhere1, strwhere2,year);
            BLL.FinanceProp f = new BLL.FinanceProp();
            var vf = f.GetAllList();
            //foreach (DataRow r in ds.Tables[0].Rows)
            //{
            //    r["se"] = decimal.Round((decimal.Parse(r["se"].ToString()) / 10000), 0);
            //}
            ViewData["FinancePropList"] = vf;
            return View(ds.Tables[0]);
          //  return View();
        }
        private string IsNull(string s1, string s2)
        {
            return string.IsNullOrEmpty(s1) ? s2 : s1;
        }
        public ActionResult ReportViewerPartial()
        {
           // var strWhere = GetWhereString();
            var zsxx = AllToData();
            MVCReportGeneratonHelper generator = new MVCReportGeneratonHelper();
            generator.CustomizeColumnsCollection += new CustomizeColumnsCollectionEventHandler(generator_CustomizeColumnsCollection);
            generator.CustomizeColumn += new CustomizeColumnEventHandler(generator_CustomizeColumn);
            XtraReport report = generator.GenerateMVCReport(OperateSession.GetFromCache("QueryAllowancePay") as MVCxGridViewState, zsxx);
            //  XtraReport report = generator.GenerateMVCReport(gridViewState, dt);

            //report.PaperKind = System.Drawing.Printing.PaperKind.A4;

            this.ViewData["rpt"] = report;

            return PartialView("ReportViewerPartial");

        }
        void generator_CustomizeColumnsCollection(object source, ColumnsCreationEventArgs e)
        {

        }
        void generator_CustomizeColumn(object source, ControlCustomizationEventArgs e)
        {

        }
        public ActionResult Print()
        {
            string nsrmc;
            string NSRBM;
            string NSRSBH;
            string year2;
            string FFinance;
            string ISPAY;
            string SKSJA;
            string SKSJB;
            GetFetchString(out nsrmc, out NSRBM, out NSRSBH, out year2, out FFinance, out ISPAY, out SKSJA, out SKSJB);
            // CreatReport(table);
            return View("Print");
        }
        public ActionResult QueryAcceptListPartial()
        {
            DataSet ds = AllToData();
            //foreach (DataRow r in ds.Tables[0].Rows)
            //{
            //    r["se"] = decimal.Round((decimal.Parse(r["se"].ToString()) / 10000), 0);
            //}
            return PartialView("QueryAcceptListPartial", ds.Tables[0]);
        }

        private DataSet AllToData()
        {
            BLL.Common CommonBLL = new BLL.Common();
            string nsrmc = Request.Params["NSRMC"];
            string NSRBM = Request.Params["NSRBM"];
            string NSRSBH = Request.Params["NSRSBH"];
            string year2 = Request.Params["FYear"];
            var FFinance = IsNull(ComboBoxExtension.GetValue<string>("FFinance1"), Request["FFinance"]);
            var ISPAY = Request.Params["ISPAY"];
            var SKSJA = Request.Params["SKSJA"];
            var SKSJB = Request.Params["SKSJB"];
            if (string.IsNullOrEmpty(year2))
            {
                year2 = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();
            }
            ViewBag.NSRMC = nsrmc;
            ViewBag.FFinance = FFinance;
            ViewBag.NSRBM = NSRBM;
            ViewBag.NSRSBH = NSRSBH;
            ViewData["year"] = year2;
            ViewData["dyear"] = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();

            //  ViewBag.FNewEnterprise = FNewEnterprise;
            // ViewBag.FIssueID = FIssueID;
            ViewBag.ISPAY = ISPAY;
            ViewBag.FYear = year2;

            // ViewBag.BTJEA = BTJEA;
            //  ViewBag.BTJEB = BTJEB;
            //ViewBag.FApplyDate = FApplyDate;
            //ViewBag.LastPayDate = LastPayDate;
            //ViewBag.ISBASE = ISBASE;
            //ViewBag.FFileNo = FFileNo;
            //if (string.IsNullOrEmpty(SKSJA))

            //{
            //    SKSJA = "400000";
            //}
            ViewBag.SKSJA = SKSJA;
            ViewBag.SKSJB = SKSJB;
            string strwhere = " 1=1 ";

            if (NSRBM != "")
            {
                strwhere += " and g.NSRBM like  '%" + NSRBM + "%'";
            }
            if (NSRSBH != "")
            {
                strwhere += " and g.NSRSBH like  '%" + NSRSBH + "%'";
            }
            if (nsrmc != "")
            {
                strwhere += " and g.NSRMC like  '%" + nsrmc + "%'";
            }
            if (year2 != "")
            {
                strwhere += " and g.AllowanceYear = " + year2;
            }
            if (!string.IsNullOrEmpty(ISPAY) && ISPAY == "true")
            {
                strwhere += " and g.accepted =  '已受理' ";
            }
            if (!string.IsNullOrEmpty(FFinance))
                strwhere += string.Format("and g.FFinancePropNo = '{0}' ", FFinance);
            if (!string.IsNullOrEmpty(SKSJA) & !string.IsNullOrEmpty(SKSJB))
            {
                strwhere += string.Format(" and g.se > '{0}'and g.se < '{1}' ", SKSJA, SKSJB);
            }
            if (!string.IsNullOrEmpty(SKSJA))
            {
                strwhere += string.Format(" and g.se > '{0}' ", SKSJA);
            }
            if (!string.IsNullOrEmpty(SKSJB))
            {
                strwhere += string.Format(" and g.se < '{0}' ", SKSJB);
            }
            //   string year = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();
            string year = year2;
            string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
            string strwhere1 = "";
            if (str != "")
            {
                strwhere1 = @"CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
 NOT in
(SELECT  CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
         FROM fund.V_SB_ZSXX WHERE" + str + @") and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            else
            {
                strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            //  strwhere += " and se>400000 ";
            ViewData["year"] = year;
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            DataSet ds = VSBZSXXBLL.GetAllowanceWaitListWithAllwance(strwhere1, strwhere, year);
            return ds;
        }


        public ActionResult ExportToXls()
        {
           
            BLL.Common CommonBLL = new BLL.Common();
            string nsrmc;
            string NSRBM;
            string NSRSBH;
            string year2;
            string FFinance;
            string ISPAY;
            string SKSJA;
            string SKSJB;
            GetFetchString(out nsrmc, out NSRBM, out NSRSBH, out year2, out FFinance, out ISPAY, out SKSJA, out SKSJB);
            DataSet ds = FieldToData(CommonBLL, nsrmc, NSRBM, NSRSBH, year2, FFinance, ISPAY, SKSJA, SKSJB); //foreach (DataRow r in ds.Tables[0].Rows)
            //{
            //    r["se"] = decimal.Round((decimal.Parse(r["se"].ToString()) / 10000), 0);
            //}
            return GridViewExtension.ExportToXls(ExportGridViewSettings, ds.Tables[0], HttpUtility.UrlEncode("已受理企业查询数据导出.xls"), true);
        }

        private void GetFetchString(out string nsrmc, out string NSRBM, out string NSRSBH, out string year2, out string FFinance, out string ISPAY, out string SKSJA, out string SKSJB)
        {
            nsrmc = Fetch.Post("hNSRMC");
            NSRBM = Fetch.Post("hNSRBM");
            NSRSBH = Fetch.Post("hNSRSBH");
            year2 = Fetch.Post("hFYear");
            FFinance = IsNull(ComboBoxExtension.GetValue<string>("FFinance1"), Fetch.Post("hFFinance"));
            ISPAY = Fetch.Post("hISPAY");
            SKSJA = Fetch.Post("hSKSJA");
            SKSJB = Fetch.Post("hSKSJB");
            if (string.IsNullOrEmpty(year2))
            {
                year2 = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();
            }
            ViewData["NSRMC"] = nsrmc;
            ViewData["FFinance"] = FFinance;
            ViewData["NSRBM"] = NSRBM;
            ViewData["NSRSBH"] = NSRSBH;
            //  ViewBag.FNewEnterprise = FNewEnterprise;
            // ViewBag.FIssueID = FIssueID;
            ViewData["ISPAY"] = ISPAY;
            ViewData["FYear"] = year2;
            // ViewBag.BTJEA = BTJEA;
            //  ViewBag.BTJEB = BTJEB;
            //ViewBag.FApplyDate = FApplyDate;
            //ViewBag.LastPayDate = LastPayDate;
            //ViewBag.ISBASE = ISBASE;
            //ViewBag.FFileNo = FFileNo;
            //if (string.IsNullOrEmpty(SKSJA))
            //{
            //    SKSJA = "400000";
            //}
            ViewData["SKSJA"] = SKSJA;
            ViewData["SKSJB"] = SKSJB;
        }

        private DataSet FieldToData(BLL.Common CommonBLL, string nsrmc, string NSRBM, string NSRSBH, string year2, string FFinance, string ISPAY, string SKSJA, string SKSJB)
        {
            string strwhere = " 1=1 ";

            if (NSRBM != "")
            {
                strwhere += " and g.NSRBM like  '%" + NSRBM + "%'";
            }
            if (NSRSBH != "")
            {
                strwhere += " and g.NSRSBH like  '%" + NSRSBH + "%'";
            }
            if (nsrmc != "")
            {
                strwhere += " and g.NSRMC like  '%" + nsrmc + "%'";
            }
            if (year2 != "")
            {
                strwhere += " and g.AllowanceYear = " + year2;
            }
            if (!string.IsNullOrEmpty(ISPAY) && ISPAY == "true")
            {
                strwhere += " and g.accepted =  '已受理' ";
            }
            if (!string.IsNullOrEmpty(FFinance))
                strwhere += string.Format("and g.FFinancePropNo = '{0}' ", FFinance);
            if (!string.IsNullOrEmpty(SKSJA) & !string.IsNullOrEmpty(SKSJB))
            {
                strwhere += string.Format(" and g.se > '{0}'and g.se < '{1}' ", SKSJA, SKSJB);
            }
            if (!string.IsNullOrEmpty(SKSJA))
            {
                strwhere += string.Format(" and g.se > '{0}' ", SKSJA);
            }
            if (!string.IsNullOrEmpty(SKSJB))
            {
                strwhere += string.Format(" and g.se < '{0}' ", SKSJB);
            }
            //string year = (decimal.Parse(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR)) - 1).ToString();
            string year = year2;
            string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
            string strwhere1 = "";
            if (str != "")
            {
                strwhere1 = @"CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
 NOT in
(SELECT  CAST(FDataSource as varchar(4000))+CAST(ZSXH as varchar(4000))
         FROM fund.V_SB_ZSXX WHERE" + str + @") and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            else
            {
                strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            // strwhere += " and se>400000 ";
            ViewData["year"] = year;
            BLL.VSBZSXX VSBZSXXBLL = new BLL.VSBZSXX();
            DataSet ds = VSBZSXXBLL.GetAllowanceWaitListWithAllwance(strwhere1, strwhere, year);

            return ds;
        }


        static GridViewSettings exportGridViewSettings;
        public static GridViewSettings ExportGridViewSettings
        {
            get
            {
                if (exportGridViewSettings == null)
                    exportGridViewSettings = CreateExportGridViewSettings();
                return exportGridViewSettings;
            }
        }

        public static GridViewSettings CreateExportGridViewSettings()
        {
            var ss = new GridViewSettings();
            ss.Name = "gvEditing";
            ss.KeyFieldName = "NSRBM";
            ss.CallbackRouteValues = new { Controller = "QueryAccept", Action = "QueryAcceptListPartial" };
            ss.Width = System.Web.UI.WebControls.Unit.Percentage(100);
            ss.SettingsPager.PageSize = EnterpriseFund.Common.GlobalConstant.CONSTRING_PAGESIZE;
            //ss.Columns.Add("NSRDZDAH", "纳税人电子档案号");
            ss.Columns.Add("NSRBM", "企业编码");
            ss.Columns.Add("NSRSBH", "纳税人识别号");
            ss.Columns.Add("NSRMC", "纳税人名称");

            ss.Columns.Add("AllowanceYear", "补贴年度");
            ss.Columns.Add("accepted", "受理状态");
            ss.Columns.Add("se", "税款金额");
            ss.Settings.ShowGroupPanel = true;
            ss.Settings.ShowFooter = true;
            ss.SettingsBehavior.AllowFocusedRow = true;
            ss.SettingsLoadingPanel.Text = "数据读取中...";
            ss.SettingsExport.Styles.Header.BackColor = System.Drawing.Color.White;
            ss.SettingsExport.Styles.Header.ForeColor = System.Drawing.Color.Black;
            ss.GroupSummary.Add(DevExpress.Data.SummaryItemType.Sum, "se").DisplayFormat = "合计:￥{0}";

            ss.ClientSideEvents.BeginCallback = "function(s,e){OnBeginCallback(s,e)}";
            ss.CommandColumn.Visible = false;

            ss.DataBinding = (sender, e) =>
            {
                ASPxGridView grid = (ASPxGridView)sender;
                for (int i = 0; i < grid.AllColumns.Count; i++)
                {
                    if (grid.AllColumns[i].HeaderStyle.HorizontalAlign == System.Web.UI.WebControls.HorizontalAlign.NotSet)
                    {
                        grid.AllColumns[i].HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                    }
                }
            };

            return ss;
        }


    }
}
